package com.teach.service.impl.photo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.teach.dto.LoginUser;
import com.teach.dto.PhotoUserInformDTO;
import com.teach.entity.Photo;
import com.teach.entity.PhotoGreat;
import com.teach.entity.PhotoPic;
import com.teach.entity.PhotoPicClass;
import com.teach.mapper.PhotoGreatMapper;
import com.teach.mapper.PhotoPicMapper;
import com.teach.service.photo.PhotoGreatService;
import com.teach.service.photo.PhotoPicService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class PhotoGreatServiceImpl implements PhotoGreatService {

	@Autowired
	private PhotoGreatMapper photoGreatMapper;

	@Autowired
	private PhotoPicService photoPicService;
	
	@Autowired
	private PhotoPicMapper photoPicMapper;

	
	@Override
	public int deleteByPrimaryKey(Long photoGreatId) {
		return 0;
	}

	@Override
	public int insert(PhotoGreatService record) {
		return 0;
	}

	@Override
	public int insertSelective(PhotoGreatService record) {
		return 0;
	}

	@Override
	public PhotoGreatService selectByPrimaryKey(Long photoGreatId) {
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(PhotoGreatService record) {
		return 0;
	}

	@Override
	public int updateByPrimaryKey(PhotoGreatService record) {
		return 0;
	}

	@Override
	public Map<String, PhotoUserInformDTO> greatInform(LoginUser user, String status) {
		//查出所有相片作品
		//List<PhotoPic> photoPics = photoPicMapper.selectByWriterId(user.getUserName());
		List<PhotoPic> photoPics = photoPicService.selectByWriterId(user.getUserName());
		
		Map<String, PhotoUserInformDTO> map = new TreeMap<String, PhotoUserInformDTO>();
		
		PhotoUserInformDTO photoPicGreatDTO = null;
		//相片集
		Map<Long, List<String>> photoAlbumMap = null;
		
		for (PhotoPic photoPic : photoPics) {
			String key = photoPic.getPhotoPicTitle();
			//包含键
			if (map.containsKey(key)) {
				photoPicGreatDTO = map.get(key);
				photoAlbumMap = photoPicGreatDTO.getPhotoAlbumMap();
			} else {
				photoPicGreatDTO = new PhotoUserInformDTO();
				photoPicGreatDTO.setTitleName(photoPic.getPhotoPicTitle());
				photoPicGreatDTO.setReleaseTime(photoPic.getPhotoReleaseTime());
				photoAlbumMap = new TreeMap<Long, List<String>>();
			}
			//图片路径（包含缩略图，水印大图）
			List<String> photoPicUrl = null;
			Long photoPicIdKey = photoPic.getPhotoPicId();
			if(photoAlbumMap.containsKey(photoPicIdKey)) {
				photoPicUrl = photoAlbumMap.get(photoPicIdKey);
			}else {
				photoPicUrl = new ArrayList<String>();
			}
			//添加缩略图
			photoPicUrl.add(photoPic.getPhotoThumbnailPicPath());
			//添加水印大图
			photoPicUrl.add(photoPic.getPhotoWatermarkPicPath());
			
			photoAlbumMap.put(photoPicIdKey, photoPicUrl);
			photoPicGreatDTO.setPhotoAlbumMap(photoAlbumMap);
			
			map.put(key, photoPicGreatDTO);
		}
		
		for (PhotoUserInformDTO dto : map.values()) {
			Set<Long> keySet = dto.getPhotoAlbumMap().keySet();
			dto.setVisitors(photoGreatMapper.selectGreatInform(keySet, user.getUserName(), status));
		}
		 
		log.debug("map ==> {}", map);
		return map;
	}

	@Override
	public int insertUpdateGreat(PhotoGreat great, PhotoPic pic) {
		great.setPhotoGreatTime(new Date());
		int insertRow = photoGreatMapper.insertSelective(great);
		PhotoPic picGreat= photoPicService.selectPicDetailsDESC(pic.getPhotoPicTitle(), pic.getPhotoWriterId(), pic.getPhotoPicClassId());		
		pic.setPhotoPicId(picGreat.getPhotoPicId());
		pic.setPhotoPicGreatCount(picGreat.getPhotoPicGreatCount()+1);
		int  updateRow = photoPicMapper.updateByPrimaryKeySelective(pic);
		return updateRow+insertRow;
	}
	

	@Override
	public PhotoGreat selectGreatId(Long picId, String picUser) {
		// TODO Auto-generated method stub
		return photoGreatMapper.selectGreatId(picId, picUser);
	}

	@Override
	public int updateStatus(String status, Long picId, String userId,PhotoPic pic) {
		int row = photoGreatMapper.updateStatus(status, picId, userId);
		PhotoPic picGreat= photoPicService.selectPicDetailsDESC(pic.getPhotoPicTitle(), pic.getPhotoWriterId(), pic.getPhotoPicClassId());		
		pic.setPhotoPicId(picGreat.getPhotoPicId());
		pic.setPhotoPicGreatCount(picGreat.getPhotoPicGreatCount()-1);
		int  updateRow = photoPicMapper.updateByPrimaryKeySelective(pic);
		return row+updateRow;
	}
	

}
